import { LunBoController } from './controllers/lunbo/lunbo.controller';
import { LunBoService } from './controllers/lunbo/lunbo.service';
import { UrlService } from './base/services/url.service';
import { QiniuController } from './base/qn-cdn/qiniu.controller';
import { QiniuService } from './base/services/qiniu.service';
import { UUIDService } from './base/services/uuid.service';
import { UserController } from './controllers/user/user.controller';
import { UserService } from './controllers/user/user.service';
import { WxSignService } from './controllers/weixin/wx.sign.service';
import { FirstController } from './controllers/first/first.controller';
import { FirstService } from './controllers/first/first.service';
import { ListController } from './controllers/list/list.controller';
import { ListService } from './controllers/list/list.service';
import {
    WxTokenStorage,
    WxliteTokenStorage,
} from './controllers/weixin/wx.token';
import { WxliteController } from './controllers/weixin/wxlite.controller';
import { WxliteService } from './controllers/weixin/wxlite.service';

import { RestfulController } from './base/restful/restful.controller';
import { RestfulService } from './base/restful/restful.service';

import { Module, Controller, CacheModule } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

import { globalConfig } from './base/config/globalConfig';
import { HttpService } from './base/services/http.service';
import { TimeTaskService } from './base/timetask/timetask.service';
import { RedisService } from './base/services/redis.service';
import { LockService } from './base/services/lock.service';

import * as mongoose from 'mongoose';
import * as redisStore from 'cache-manager-redis-store';

// import glob = require('glob')
// import path = require('path')

// const rootpath = path.normalize(__dirname + '/..');

// const routers = glob.sync(rootpath + '/src/controllers/**/*.controller.ts');
// console.log(routers)
// console.log('=====')
// const my_controllers = routers
//     .map(file => {
//         const route = require(file);
//         console.log('route===')
//       console.log(route)
//       console.log('route===')
//         const controller_name = Object.keys(route).find(v =>{

//           console.log('vvvvv')
//           console.log(v);
//           return v.endsWith('Controller')
//         }
//         );
//         return route[controller_name];
//     })
//     .filter(f => {
//         return f != null;
//     });
//     console.log(my_controllers)
// const services = glob.sync(rootpath + '/src/controllers/**/*.service.ts');
// const my_services:any[] = services
//     .map(file => {
//         const service = require(file);
//         const service_name = Object.keys(service).find(v =>
//             v.endsWith('Service'),
//         );
//         return service[service_name];
//     })
//     .filter(f => {
//         return f != null;
//     });

export const ServiceDict = {
    UUIDService,
    QiniuService,
    UrlService,
    AppService,
    HttpService,
    RestfulService,
    TimeTaskService,
    LockService,
    RedisService,
    FirstService,
    ListService,
    WxliteService,
    WxTokenStorage,
    WxliteTokenStorage,
    WxSignService,
    UserService,
    LunBoService,
};
export const ControllerDict = {
    QiniuController,
    AppController,
    RestfulController,
    FirstController,
    ListController,
    WxliteController,
    UserController,
    LunBoController,
};
@Module({
    imports: [
        CacheModule.register({
            store: redisStore,
            ttl: 24 * 3600,
            max: 100,
            host: RedisService.REDIS_CONFIG.host,
            auth_pass: RedisService.REDIS_CONFIG.password,
            db: RedisService.REDIS_CONFIG.db,
        }),
    ],
    controllers: [...Object.values(ControllerDict)],
    providers: [...Object.values(ServiceDict)],
})
export class AppModule {
    private connectToMongodb() {
        mongoose.connect(
            globalConfig.database,
            {
                useNewUrlParser: true,
                useFindAndModify: false,
            },
            err => {
                console.log(
                    err
                        ? `connect mongodb error: ${err}`
                        : 'connected to mongodb',
                );
            },
        );
    }
    constructor(
        private timeTaskService: TimeTaskService,
        private redisService: RedisService,
    ) {
        this.connectToMongodb();
        this.timeTaskService.startGlobalTask();
    }
}
